package cn.iocoder.yudao.module.mzt.controller.admin.articleinteraction;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.mzt.controller.admin.articleinteraction.vo.*;
import cn.iocoder.yudao.module.mzt.dal.dataobject.articleinteraction.ArticleInteractionDO;
import cn.iocoder.yudao.module.mzt.service.articleinteraction.ArticleInteractionService;

@Tag(name = "管理后台 - 文章交互记录")
@RestController
@RequestMapping("/mzt/article-interaction")
@Validated
public class ArticleInteractionController {

    @Resource
    private ArticleInteractionService articleInteractionService;

    @PostMapping("/create")
    @Operation(summary = "创建文章交互记录")
    @PreAuthorize("@ss.hasPermission('mzt:article-interaction:create')")
    public CommonResult<Long> createArticleInteraction(@Valid @RequestBody ArticleInteractionSaveReqVO createReqVO) {
        return success(articleInteractionService.createArticleInteraction(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新文章交互记录")
    @PreAuthorize("@ss.hasPermission('mzt:article-interaction:update')")
    public CommonResult<Boolean> updateArticleInteraction(@Valid @RequestBody ArticleInteractionSaveReqVO updateReqVO) {
        articleInteractionService.updateArticleInteraction(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除文章交互记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('mzt:article-interaction:delete')")
    public CommonResult<Boolean> deleteArticleInteraction(@RequestParam("id") Long id) {
        articleInteractionService.deleteArticleInteraction(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得文章交互记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('mzt:article-interaction:query')")
    public CommonResult<ArticleInteractionRespVO> getArticleInteraction(@RequestParam("id") Long id) {
        ArticleInteractionDO articleInteraction = articleInteractionService.getArticleInteraction(id);
        return success(BeanUtils.toBean(articleInteraction, ArticleInteractionRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得文章交互记录分页")
    @PreAuthorize("@ss.hasPermission('mzt:article-interaction:query')")
    public CommonResult<PageResult<ArticleInteractionRespVO>> getArticleInteractionPage(@Valid ArticleInteractionPageReqVO pageReqVO) {
        PageResult<ArticleInteractionDO> pageResult = articleInteractionService.getArticleInteractionPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ArticleInteractionRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出文章交互记录 Excel")
    @PreAuthorize("@ss.hasPermission('mzt:article-interaction:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportArticleInteractionExcel(@Valid ArticleInteractionPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ArticleInteractionDO> list = articleInteractionService.getArticleInteractionPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "文章交互记录.xls", "数据", ArticleInteractionRespVO.class,
                        BeanUtils.toBean(list, ArticleInteractionRespVO.class));
    }

}